package com.wyh.controller;


import com.wyh.common.annotation.Log;
import com.wyh.common.annotation.RateLimiter;
import com.wyh.common.enums.BusinessType;
import com.wyh.common.response.PageInfo;
import com.wyh.common.result.R;
import com.wyh.common.util.ExcelUtils;
import com.wyh.entity.MsgChatRoom;
import com.wyh.request.MsgChatRoomRequest;
import com.wyh.service.IMsgChatRoomService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

/**
 * 群组控制层
 *
 * @author WangYuHang
 * @date 2024-03-08
 */
@Slf4j
@AllArgsConstructor
@RestController
@Api(value = "群组接口管理",tags = "群组接口管理")
@RequestMapping("/admin/msgChatRoom" )
public class MsgChatRoomController {

    private final IMsgChatRoomService iMsgChatRoomService;


    /**
     * 获取群聊列表
     */
    @ApiOperation(value = "获取群聊列表")
    @GetMapping
    public R<List<MsgChatRoom>> query() {
        R<List<MsgChatRoom>> listR = iMsgChatRoomService.queryList();
        return listR;
    }

    /**
     * 根据id获取群聊
     */
    @ApiOperation(value = "根据id获取群聊")
    @GetMapping("/{chatRoomId}")
    public R<MsgChatRoom> queryId(@PathVariable("chatRoomId") String chatRoomId) {
        R<MsgChatRoom> msgChatRoomR = iMsgChatRoomService.queryById(chatRoomId);
        return msgChatRoomR;
    }

    /**
     * 创建群聊
     */
    @Log(title = "创建群聊", businessType = BusinessType.INSERT)
    @RateLimiter
    @ApiOperation(value = "创建群聊")
    @PostMapping("/add")
    public R<Void> createChatRoom(@RequestBody MsgChatRoomRequest msgChatRoomRequest) {
        iMsgChatRoomService.add(msgChatRoomRequest);
        return R.success();
    }

    /**
     * 解散群聊
     */
    @ApiOperation(value = "解散群聊")
    @DeleteMapping("/dissolveChatRoom/{chatRoomId}")
    public R<String> dissolveChatRoom(@PathVariable String chatRoomId) {
        R<String> stringR = iMsgChatRoomService.dissolveChatRoom(chatRoomId);
        return stringR;
    }

    /**
     * 将用户移出群聊
     */
    @ApiOperation(value = "将用户移出群聊")
    @DeleteMapping("/{chatRoomId}/{userId}")
    public R<String> moveOutUserByChatRoom(@PathVariable("chatRoomId") String chatRoomId,@PathVariable("userId") String userId) {
        R<String> stringR = iMsgChatRoomService.moveOutChatRoom(chatRoomId, userId);
        return stringR;
    }


    /**
     * 分页查询群组列表
     */
    @ApiOperation(value = "分页查询群组列表")
    @PostMapping("/pageList")
    public R<PageInfo<MsgChatRoom>> pageList(@RequestBody MsgChatRoomRequest msgChatRoomRequest) {
        return R.success(iMsgChatRoomService.pageList(msgChatRoomRequest));
    }

    /**
     * 获取群组详细信息
     */
    @ApiOperation(value = "获取群组详细信息")
    @GetMapping(value = "/query/{id}")
    public R<MsgChatRoom> detail(@PathVariable("id") String id) {
        return R.success(iMsgChatRoomService.getById(id));
    }





    /**
     * 修改群组
     */
    @Log(title = "修改群组", businessType = BusinessType.UPDATE)
    @ApiOperation(value = "修改群组")
    @PostMapping("/update")
    public R<Void> edit(@RequestBody MsgChatRoomRequest msgChatRoomRequest) {
        iMsgChatRoomService.update(msgChatRoomRequest);
        return R.success();
    }

    /**
     * 删除群组
     */
    @Log(title = "删除群组", businessType = BusinessType.DELETE)
    @ApiOperation(value = "删除群组")
    @PostMapping("/delete/{ids}")
    public R<Void> remove(@PathVariable Integer[] ids) {
        iMsgChatRoomService.deleteBatch(Arrays.asList(ids));
        return R.success();
    }

    /**
    * 导出群组
    *
    * @param response response
    * @throws IOException IOException
    */
    @PostMapping(value = "/export")
    public void exportExcel(HttpServletResponse response,@RequestBody MsgChatRoomRequest msgChatRoomRequest) throws IOException {
        List<MsgChatRoom> list = iMsgChatRoomService.list(msgChatRoomRequest);
        ExcelUtils.exportExcel(list, MsgChatRoom.class, "群组信息", response);
    }
}
